<?php
	session_start();	
	
	require_once('./php/user.api.php');
	
	if($_SERVER['REQUEST_METHOD']=='POST')
	{
		$resu = getUsers();
		$res = verifyUserPHP($_POST['usernm'], $_POST['userpw']);
		
		// 성공 시
		if($res[result] == 'success')
		{
			// 이름과 id를 세션 변수로 저장
			$_SESSION['usernm'] = $_POST['usernm'];
			$_SESSION['userid'] = $res[id];
			header("Location: main.php");
		}
		else {
			// 로그인 실패
		}
	}
?>
<!DOCTYPE html>
<html lang="ko"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>LifeQuest Log-in page</title> 
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />

<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="css/bootstrap-responsive.css" type="text/css" />
<link rel="stylesheet" href="css/style.css" type="text/css" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/lifequest.js"></script>


<!--[if lt IE 9]>
	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>	
<![endif]-->

<script type="text/javascript">
	$(document).ready(function() {
		
		if($.cookie("rememberme"))
		{
			$("#chk_rmb").attr("checked", "checked");
			if($.cookie("usernm")) {
				$("#input_nm").val($.cookie("usernm"));
			}
		}
						
		$("#btn_signin").click(function() {
			if($("#chk_rmb").prop('checked'))
			{
				$.cookie('rememberme', $("#chk_rmb").prop('checked') ,{expires:300});
				$.cookie('usernm', $("#input_nm").val() ,{expires:300});
			}
			else {
				$.cookie('rememberme', null);
				$.cookie('usernm', null);
			}
			
		});
		
		$("#btn_signup").click(function() {
			signup($("#input_su_nm"), $("#input_su_pw"),
					function() {
						alert("가입했습니다.");
						$("#modalSignup").modal('hide');
					});
		});
				
		$("#modalSignup").on('hidden', function(e) {
			if ($(e.target).hasClass("modal")) {
				initiate_input([$("#input_su_nm"), $("#input_su_pw")]);
				$("#input_su_nm").popover('hide');
				$("#input_su_pw").popover('hide');				
			}			
		});
		
		$("#modalSignup").on('shown', function(e) {
			if ($(e.target).hasClass("modal")) {
				$("#input_su_nm").focus();				
			}			
		});
		
		$("#input_su_nm").bind('input', function() {
			$("#input_su_nm").popover('hide');
			$(this).focus();
		});
		$("#input_su_pw").bind('input', function() {
			$("#input_su_pw").popover('hide');
			$(this).focus();
		});
		
	});
	
	
</script>

</head>
<body>
	<div class="container ">
		<form class="form-signin" method="post" action="login.php">
			<h2 style="text-align: center">Please Sign In</h2>
			<div class="input-prepend" >
				<span class="add-on"><i class="icon-user"></i></span>
				<input name="usernm" id="input_nm" type="text" placeholder="name" >	
			</div>
			<div class="input-prepend" >
				<span class="add-on"><i class="icon-lock"></i></span>
				<input name="userpw" id="input_pw" type="password" placeholder="password">
			</div>
			<label class="checkbox" >
				<input id="chk_rmb" type="checkbox" ><span>이름기억하기</span>
			</label>
			<button id="btn_signin" class="btn btn-large btn-primary btn-block" type="submit">Sign in</button>
			<a class="btn btn-block" href="#modalSignup" role="button" data-toggle="modal">가입하기</a>
		</form>
	</div>
	<div id="modalSignup" class="modal hide fade" tabindex="-1" role="dialog" >
		<div class="modal-header" style="text-align: center;">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true" tabindex="-1">×</button>
    		<h4 id="myModalLabel">회원가입</h4>
		</div>
		<div class="modal-body">
			<div class="control-group">
				<div class="controls">
					<div class="input-prepend" >
						<span class="add-on"><i class="icon-user"></i></span>
						<input name="nm" id="input_su_nm" type="text" class="input-block-level" placeholder="name"
						data-trigger="manual" data-placement="bottom" data-content="사용할 수 없는 이름입니다." >	
					</div>					
				</div>
			</div>
		    <div class="control-group">
				<div class="controls">
					<div class="input-prepend" >
						<span class="add-on"><i class="icon-user"></i></span>
						<input name="pw" id="input_su_pw" type="password" class="input-block-level" placeholder="password"
						data-trigger="manual" data-placement="top" data-content="비밀번호는 6자리 이상!" >	
					</div>
					
				</div>
			</div>				
	  	</div>
	  	<div class="modal-footer">
		    <!-- <button class="btn" data-dismiss="modal" aria-hidden="true">닫기</button> -->
		    <button id="btn_signup" class="btn btn-large btn-primary btn-block" >가입하기</button>
	  	</div>
	</div>
</body>
</html>
